package cn.kys.modules.system.service.impl;


import cn.kys.modules.system.repository.ToolsResposity;
import cn.kys.modules.system.service.LayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LayerServiceImpl implements LayerService {
    @Autowired
    private ToolsResposity toolsResposity;

    @Autowired
    MongoTemplate mongoTemplate;
    @Override
    public void addLayer(String name, String cname, String url,String type) {
        String sql_find = "select * from t_layer where name='" + name + "'" + "or cname='" + cname + "'";
        Map map = toolsResposity.exeSql(sql_find);
        if (map == null) {
            String sql_insert = "INSERT INTO public.t_layer(name,cname,url,type) VALUES ('" + name + "','" + cname +"','"+url+ "','"+type+"');";
            toolsResposity.exeSql(sql_insert);
        }
    }

    @Override
    public Map editLayer(String id, String name, String cname, String url, String type) {
        String sql = "UPDATE t_layer SET cname = '" + cname + "',name='" + name +"',url='"+ url + "',type='"+type+"' WHERE id ='" + id + "'";
        toolsResposity.exeSql(sql);
        return null;
    }


    @Override
    public List getAllLayers() {
        /*System.out.println("aaaa");
        int size = mongoTemplate.getCollectionNames().size();*/

        // wkt转geoJson
        /*try {
            String wktPoint = "POINT(11.11111 12.22222)";
            WKTReader reader = new WKTReader();
            Geometry geometry = reader.read(wktPoint);

            StringWriter writer = new StringWriter();
            FeatureJSON f = new FeatureJSON();

            GeometryJSON g = new GeometryJSON();
            g.write(geometry, writer);
            String result = writer.toString();
            System.out.println("GeoJson结果=" + result);
        } catch (Exception e) {
            System.out.println("转换异常!" + e.toString());
        }*/

        String sql = "SELECT * FROM public.t_layer";
        return toolsResposity.exeSqlList(sql);

    }

    @Override
    public Map getAllTypeLayers() {
        Map map = new HashMap();

        String sql_dt = "SELECT * FROM public.t_layer where type = '底图'";
        List listDT = toolsResposity.exeSqlList(sql_dt);

        String sql_ztt = "SELECT * FROM public.t_layer where type = '专题图'";
        List listZTT = toolsResposity.exeSqlList(sql_ztt);

        String sql_gzt = "SELECT * FROM public.t_layer where type = '工作图'";
        List listGZT = toolsResposity.exeSqlList(sql_gzt);

        map.put("dt",listDT);
        map.put("ztt",listZTT);
        map.put("gzt",listGZT);

        return map;
    }

    @Override
    public Map delLayerById(String id) {

        Map returnMap = new HashMap();

        String sqlLayer = "select * from t_layer where id = '"+id+"'";
        Map layerMap = toolsResposity.exeSql(sqlLayer);
        String type = layerMap.get("type").toString();

        String sql = "";

        if("底图".equals(type)){
            //底图
            sql = "select * from t_case_baselayer where layerid in (" + id + ")";
            List caseLayerMap = toolsResposity.exeSqlList(sql);
            if (caseLayerMap.size() > 0) {
                //不能删
                returnMap.put("deleted", false);
                returnMap.put("msg", "方案表中使用了该图层，不能删除");
            } else {
                //可以删除
                String sql_delete = "delete from t_layer where id in (" + id + ")";
                toolsResposity.exeSql(sql_delete);
                returnMap.put("deleted", true);
            }
        }else if("专题图".equals(type)){
            //专题图
            sql = "select * from t_case_speciallayer where layerid in (" + id + ")";
            List caseLayerMap = toolsResposity.exeSqlList(sql);
            if (caseLayerMap.size() > 0) {
                //不能删
                returnMap.put("deleted", false);
                returnMap.put("msg", "方案表中使用了该图层，不能删除");
            } else {
                //可以删除
                String sql_delete = "delete from t_layer where id in (" + id + ")";
                toolsResposity.exeSql(sql_delete);
                returnMap.put("deleted", true);
            }
        }else{
            //工作图
            sql = "select * from t_case_editlayer where layerid in (" + id + ")";
            List caseLayerMap = toolsResposity.exeSqlList(sql);
            if (caseLayerMap.size() > 0) {
                //不能删
                returnMap.put("deleted", false);
                returnMap.put("msg", "方案表中使用了该图层，不能删除");
            } else {
                //可以删除
                String sql_delete = "delete from t_layer where id in (" + id + ")";
                toolsResposity.exeSql(sql_delete);
                returnMap.put("deleted", true);
            }
        }



        return returnMap;
    }
}

